package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sumpu.app.guoguojie.dao.LuckyDao;
import cn.sumpu.app.guoguojie.domain.LuckRank;
import cn.sumpu.app.guoguojie.domain.LuckTime;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class LuckyDaoImpl extends SqlSessionDaoSupport implements LuckyDao {

    @Override
    public void insertLuckTime(LuckTime luckTime) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.insertLuckTime", luckTime);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertLuckTime failure.", e);
            }
            throw new AppBusinessException("insertLuckTime failure.", e);
        }
    }

    @Override
    public void insertLuckRank(LuckRank luckRank) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.insertLuckRank", luckRank);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertLuckRank failure.", e);
            }
            throw new AppBusinessException("insertLuckRank failure.", e);
        }
    }

    @Override
    public void updateLuckTime(LuckTime luckTime) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.updateLuckTime", luckTime);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateLuckTime failure.", e);
            }
            throw new AppBusinessException("updateLuckTime failure.", e);
        }
    }

    @Override
    public void updateLuckRank(LuckRank luckRank) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.updateLuckRank", luckRank);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateLuckRank failure.", e);
            }
            throw new AppBusinessException("updateLuckRank failure.", e);
        }
    }

    @Override
    public void deleteLuckTime(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.deleteLuckTime", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteLuckTime failure.", e);
            }
            throw new AppBusinessException("deleteLuckTime failure.", e);
        }
    }

    @Override
    public void deleteLuckRank(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.deleteLuckRank", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteLuckRank failure.", e);
            }
            throw new AppBusinessException("deleteLuckRank failure.", e);
        }
    }

    @Override
    public int getLuckTimeCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.getLuckTimeCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getLuckTimeCount failure.", e);
            }
            throw new AppBusinessException("getLuckTimeCount failure.", e);
        }
    }

    @Override
    public int getLuckRankCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.getLuckRankCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getLuckRankCount failure.", e);
            }
            throw new AppBusinessException("getLuckRankCount failure.", e);
        }
    }

    @Override
    public String getLastTime(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.getLastTime", map);
            return (String) (obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMinTime failure.", e);
            }
            throw new AppBusinessException("getMinTime failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<LuckTime> getLuckTime(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<LuckTime>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.getLuckTime", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getLuckTime failure.", e);
            }
            throw new AppBusinessException("getLuckTime failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<LuckRank> getLuckRank(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<LuckRank>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.getLuckRank", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getLuckRank failure.", e);
            }
            throw new AppBusinessException("getLuckRank failure.", e);
        }
    }

    @Override
    public int getMinRank(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.LuckyRankMapper.getMinRank", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMinRank failure.", e);
            }
            throw new AppBusinessException("getMinRank failure.", e);
        }
    }
}
